﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Library
{
    public class Book : Reading, IBorrowable
    {
        IStorable dbBorrowClients = new DatabaseProxy();

        public Genre BookGenre { get; set; }

        public ulong ClientBorrowIt { get; set; }

        public Book(string title, string author, bool eCopy, string iDNumber, DepartmentsName departmentLocation, Genre genre)
            : base(title, author, iDNumber, eCopy, departmentLocation)
        {
            this.BookGenre = genre;
        }

        public void AddNewBorrow(Client clientBorrowIt)
        {
            this.available = false;
            this.EndBorrowPeriod = DateTime.Today.AddDays(30);
            this.ClientBorrowIt = clientBorrowIt.EGN;
            clientBorrowIt.Taken.Add(this);

            dbBorrowClients.StoreTemp(this, clientBorrowIt);
        }

        public void ReturnBorrow(Client clientReturnIt)
        {
            this.available = true;
            this.EndBorrowPeriod = default(DateTime);
            this.ClientBorrowIt = default(long);
            clientReturnIt.Taken.Remove(this);

            dbBorrowClients.StoreTemp(this, clientReturnIt);
            dbBorrowClients.RemoveTemp(this);
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(this.Title + "; " + this.Author);
            sb.AppendLine(this.ECopy + "; " + this.IDNumber + "; " + this.DepartmentLocation + "; " + this.BookGenre);
            sb.AppendLine(this.ClientBorrowIt + "; " + this.available + "; " + this.EndBorrowPeriod);

            return sb.ToString();
        }
    }
}
